package com.client.koshercook.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.user.client.rpc.IsSerializable;


/** iterator will iterate over the alternative ingredients (strings) */
@PersistenceCapable
public class ingWithAlters implements Iterable<Key>,IsSerializable{

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    
    @Persistent
    private Key baseIng;	//name of ingredient
	    
    @Persistent
    private ArrayList<Key> alterIngs;	//a set of alternative ingredients

    @Persistent
    private boolean isGroup;
    
    /** add alter ingredient. alter is the first alternative */
	public ingWithAlters(Key baseIng, Key alter) {
		alterIngs = new ArrayList<Key>();
		this.baseIng = baseIng;
		alterIngs.add(alter);
		this.setGroup(false);
	}
	
	 /** alter from group ingredients (when scanning recipe)*/
	public ingWithAlters(boolean b,String baseIng, String... alter) {
		alterIngs = new ArrayList<Key>();
		this.setGroup(true);
		this.baseIng = KeyFactory.createKey(Ingredient.class.getSimpleName(), baseIng);
		for(String s : alter)
			alterIngs.add(KeyFactory.createKey(Ingredient.class.getSimpleName(), s));
	} 
	
	/** constructor to get a copy of ingWithAlters */
	public ingWithAlters(ingWithAlters iwa) {
		alterIngs = new ArrayList<Key>();
		this.baseIng = iwa.getBaseIng();	
		alterIngs.addAll(iwa.getAllAlters());
		this.setGroup(iwa.isGroup());
	}
	
	public Key getKey() {
        return key;
    }
	
	public Key getBaseIng() {
		return baseIng;
	}
	
	public void addAlter(Key alter) {
		alterIngs.add(alter);
	}
	
	public boolean isAlter(Key alter) {
		return alterIngs.contains(alter);
	}
	
	public ArrayList<Key> getAllAlters() {
		ArrayList<Key> newAlterIngs = alterIngs;
		return newAlterIngs;
	}
	
	@Override
	public Iterator<Key> iterator() {
		return new myIterator();
	}

	/* use this iterator to get all the alternatives */
	private class myIterator implements Iterator<Key>{

		Iterator<Key> innerIterator;
		
		private myIterator(){
			innerIterator = alterIngs.iterator();
		}
		
		@Override
		public boolean hasNext() {
			return innerIterator.hasNext();
		}

		@Override
		public Key next() {
			return innerIterator.next();
		}

		@Override
		public void remove() {
			innerIterator.remove();
		}
	}

	/** return an empty list of alters. if ingredients contains groups, it adds them to the list*/
	public static List<ingWithAlters> setAltersForGroups(List<Key> ingredients) {
		List<ingWithAlters> res = new ArrayList<ingWithAlters>();
		String ingName;
		for(Key k : ingredients) {
			ingName = k.getName();
			if(ingName.equals("יין")) res.add(new ingWithAlters(true,"יין","יין קינוח","יין מחוזק","יין מבעבע","יין לבן","יין אדום"));
			else if(ingName.equals("חומץ")) res.add(new ingWithAlters(true,"חומץ","חומץ אורז","חומץ בלסמי","חומץ בן יין","חומץ לבן","חומץ פטל","חומץ שרי","חומץ תפוחים"));
			else if(ingName.equals("שמן")) res.add(new ingWithAlters(true,"שמן","שמן אגוזי לוז","שמן אגוזי מלך","שמן בוטנים","שמן דקלים","שמן זית","שמן זרעי ענבים","שמן חמניות","שמן כותנה","שמן חריע","שמן סויה","שמן צ'ילי","שמן קוקוס","שמן קנולה","שמן שומשום","שמן תירס"));
			else if(ingName.equals("אגוזים")) res.add(new ingWithAlters(true,"אגוזים","אגוז ברזיל","אגוז לוז","אגוז מוסקט","אגוז מלך","אגוז מקדמיה","אגוז פקאן","אגוז קשיו"));
			else if(ingName.equals("לחם")) res.add(new ingWithAlters(true,"לחם","לחם אורגני","לחם אחיד","לחם דגנים","לחם חי","לחם לבן","לחם קל","לחם שאור","לחם שיפון","לחם שחור"));

		}
    	return res;
	}
	
	public String toString() {
		String str = "במקום " + this.baseIng.getName() + " אפשר: ";
		if(isGroup) str = str + "סוגי "+ this.baseIng.getName() + " שונים.";
		else for(Key k : alterIngs) str = str + " " + k.getName()+",";
		return str.substring(0,str.length()-1) + ".";
	}

	public void setGroup(boolean isGroup) {
		this.isGroup = isGroup;
	}

	public boolean isGroup() {
		return isGroup;
	}
}
